﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FlowDesigner.HelperVision
{
    public class Tool
    {
       public static void MapProperties(object source, object target)
        {
            // 获取源对象和目标对象的类型
            Type sourceType = source.GetType();
            Type targetType = target.GetType();

            // 获取目标对象的所有可写属性
            var targetProperties = targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(p => p.CanWrite);
            foreach (var targetProp in targetProperties)
            {
                // 在源对象中找到名称完全相同的属性
                var sourceProp = sourceType.GetProperty(targetProp.Name, BindingFlags.Public | BindingFlags.Instance);

                if (sourceProp != null && sourceProp.CanRead && targetProp.PropertyType.IsAssignableFrom(sourceProp.PropertyType))
                {
                    // 获取源对象属性值并赋值到目标对象属性
                    var value = sourceProp.GetValue(source);
                    targetProp.SetValue(target, value);
                }
            }
        }
    }
}
